<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Dubbo</title>
    <script type="text/javascript" src="jquery-easyui-1.4.5/jquery.min.js"></script>
    <script type="text/javascript" src="jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/public.css">
    <script type="text/javascript" src="jquery-easyui-1.4.5/function/sosowx.js?v=0.2"></script>
    <script type="text/javascript">

        var typeDate;
        var typeid;
        var level;
        var base;
        /**
         * 初始化分类列表
         */
        function init() {
            $.ajax({
                type: "POST",
                url: "/metadata/metadataQuery/getList/"+Table.META_TYPE,
                async: true,
                dataType: 'json',
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    typeDate = data;
                    $("#parentPoint").html("<li><a href=\"#\" onclick=\"initDateList(0)\">元数据分类</a></li>");
                    for (i = 0; i < data.length; i++) {

                        var li = "";
                        if (data[i].type_level == 1) {
                            li = "<li><a href=\"#\" onclick=\"initDateList(" + (i * 1 + 1) + ")\">" + data[i].type_name + " </a>";
                            var ul = "<ul>";
                            for (j = 0; j < data.length; j++) {
                                if (data[j].type_parent == data[i].type_id) {
                                    ul += "<li><a href=\"#\" onclick=\"initDateList(" + (j * 1 + 1) + ")\">" + data[j].type_name + " </a></li>";
                                }
                            }
                            ul += "</ul>";
                            li += ul;
                            li + "</li>";
                        }
                        $("#parentPoint").append(li);
                    }
                }
            });
            getBaseList();
        }
        /**
         * 初始化元数据列表
         */
        function initDateList(index) {
            if (index == 0)typeid = 0;
            else {
                typeid = typeDate[index - 1].type_id;
                level = typeDate[index - 1].type_level;
                getDataList(typeid);
            }
        }
        /**
         * 获取元数据列表
         */
        function getDataList(typeId) {
            var data = {
              filter:{
                  type_id:typeId
              }
            };
            $.ajax({
                type: "POST",
                url: "/metadata/metadataQuery/getList/"+Table.META_DATA,
                async: true,
                dataType: 'json',
                data:JSON.stringify(data),
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    $("#metaList").html("<thead> <td>元数据名称</td> <td>分类ID</td> <td>操作</td> </thead>");
                    for (i = 0; i < data.length; i++) {
                        var table = "<tr>";
                        table += "<td>" + data[i].data_name + "</td>"
                                + "<td>" + data[i].type_id + "</td>"
                                + "<td><a href=\"updateMetaData.html?id=" + data[i].data_id + "\">维护元数据</a>&nbsp;"
                                + "<a href=\"updateModel.html?id=" + data[i].data_id + "\">维护应用模板</a></td>";
                        table += "</tr>";
                        $("#metaList").append(table);
                    }
                }
            });
        }
        /**
         * 获取数据库列表
         */
        function getBaseList() {
            $.ajax({
                type: "POST",
                url: "/metadata/metadataQuery/getList/"+Table.META_DATA_BASE,
                async: true,
                dataType: 'json',
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    base = data;
                    $("#baseList").html("<thead> <td>数据库名称</td> <td>数据库类型</td> <td>服务器IP</td> <td>用户名</td> <td>密码</td><td>数据源配置id</td>  <td>操作</td> </thead>");
                    $("#database").append("");
                    for (i = 0; i < data.length; i++) {
                        var table = "<tr>";
                        table += "<td>" + data[i].base_name + "</td>"
                                + "<td>" + data[i].base_type + "</td>"
                                + "<td>" + data[i].base_ip + "</td>"
                                + "<td>" + data[i].base_user + "</td>"
                                + "<td>" + data[i].base_password + "</td>"
                                + "<td>" + data[i].base_data_source + "</td>"
                                + "<td><a href=\"updateTable.html?id=" + data[i].base_id + "\">维护该库数据字典</a></td>";
                        table += "</tr>";
                        $("#baseList").append(table);
                        $("#database").append("<option value=\""+i+"\">"+data[i].base_name+"</option>");
                    }
                }
            });
        }

        /**
         * 保存类型
         */
        function saveType() {
            var thislevel, parent;
            if (typeid == 0) {
                thislevel = 1;
                parent = 0;
            } else {
                thislevel = level + 1;
                parent = typeid;
            }
            var datas = {
                type_name: $("#typename").val(),
                type_level: thislevel,
                type_parent: parent
            }
            $.ajax({
                type: "POST",
                url: "/metadata//metadataNew/insert/" + Table.META_TYPE,
                async: true,
                dataType: 'json',
                data: JSON.stringify(datas),
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    init();
                }
            });
        }
        /**
         * 保存元数据
         */
        function saveData() {
            var datas = {
                data_name: $("#dataname").val(),
                type_id: typeid,
                data_source_id: base[ $("#database").val()].base_data_source,
                base_id: base[ $("#database").val()].base_id,
            }
            $.ajax({
                type: "POST",
                url: "/metadata//metadataNew/insert/" + Table.META_DATA,
                async: true,
                dataType: 'json',
                data: JSON.stringify(datas),
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    getDataList(typeid);
                }
            });
        }

        /**
         * 保存数据库字典
         */
        function saveBase() {
            var datas = {
                base_name: $("#baseName").val(),
                base_type: $("#baseType").val(),
                base_data_source: $("#dsid").val(),
                base_ip: $("#baseIp").val(),
                base_user: $("#baseUser").val(),
                base_password: $("#basePassword").val()
            }
            $.ajax({
                type: "POST",
                url: "/metadata//metadataNew/insert/" + Table.META_DATA_BASE,
                async: true,
                dataType: 'json',
                data: JSON.stringify(datas),
                timeout: 5000,
                contentType: 'application/text;charset=UTF-8',
                success: function (data) {
                    getBaseList();
                }
            });
        }

    </script>
</head>
<body onload="init();">
<div style="width: auto; float: left;height: 960px;overflow: auto;">
    <h6>维护元数据分类</h6>
    <label>类型名称</label><input type="text" id="typename"><input type="button" value="新增类型" onclick="saveType()">
    <ul id="parentPoint">
    </ul>
</div>
<div style="width: auto;float: left;height: 960px;overflow: auto;">
    <h6>维护元数据</h6>

    <p/>
    <label>元数据名称</label><input type="text" id="dataname"><p/><label>所属数据库</label><select id="database"></select>
    <input type="button" value="新增元数据" onclick="saveData()">

    <p/>
    <table id="metaList">
        <thead>
        <td>元数据名称</td>
        <td>数据源配置ID</td>
        <td>分类ID</td>
        <td>操作</td>
        </thead>
    </table>
</div>
<div style="width: auto;float: left;">
    <h6>维护数据库字典表</h6>
    数据库名称：<input type="text" id="baseName">数据库类型：<input type="text" id="baseType">数据源配置ID：<input type="text" id="dsid">

    <p/>
    服务器ip：<input type="text" id="baseIp">用户名：<input type="text" id="baseUser">密码：<input type="text" id="basePassword">

    <p/>
    <input type="button" value="新增数据库" onclick="saveBase()">

    <p/>
    <table id="baseList">
        <thead>
        <td>数据库名称</td>
        <td>数据库类型</td>
        <td>数据源配置ID</td>
        <td>服务器IP</td>
        <td>用户名</td>
        <td>密码</td>
        <td>操作</td>
        </thead>
    </table>
</div>
</body>
</html>